"""
Case Type   : CM
Case Name   : 改参数agent_check_interval值为非法值
Create At   : 2024.6
Owner       : haomeng802
Description :
    1、查看集群状态 cm_ctl query -Cidvp
    2、查询参数agent_check_interval默认值
    3、修改参数值为-1（上边界）
    4、修改参数值为2147483648（下边界）
    5、修改参数值为空（特殊字符）
    6、修改参数值为null（文本类型）
Expect      :
    1、集群状态正常，数据库节点状态正常
    2、默认值查询成功为2
    3、设置失败，合理报错
    4、设置失败，合理报错
    5、设置失败，合理报错
    6、设置失败，合理报错
History     :
    2024.4.11 ningyali 增加前置（安装cm）和后置（卸载cm）操作
"""

import os
import unittest

from testcase.utils.ComInstall import CmInstall
from testcase.utils.ComInstall import CmUnInstall
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class CmServer(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.param = 'agent_check_interval'
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.root_nodes = (Node('PrimaryRoot'), Node('Standby1Root'),
                           Node('Standby2Root'))
        self.user_nodes = (Node('PrimaryDbUser'), Node('Standby1DbUser'),
                           Node('Standby2DbUser'))
        self.common = Common()

        text = '----prestep1:安装cm;expect:执行成功----'
        self.log.info(text)
        self.install_pkg_path = os.path.dirname(macro.DB_SCRIPT_PATH)
        self.cm = CmInstall(self.install_pkg_path, self.pri_sh.node.ssh_user)
        res = self.cm.update_xml_for_cm(*self.root_nodes)
        self.assertGreater(res.count('cmDir'), 1, f'执行失败: {text}')

        res = self.cm.cm_install(self.user_nodes[0])
        self.assertIn('Install CM tool success', res, f'执行失败: {text}')

    def test_cm_server(self):
        text = '-----step1:查看集群状态; expect:集群状态正常-----'
        self.log.info(text)
        status = self.pri_sh.exec_cm_ctl(mode='query', param='-Cv')
        self.log.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败' + text)

        text = '----step2:查询参数agent_check_interval默认值; expect:默认值查询成功为2----'
        self.log.info(text)
        cmd = f'source {macro.DB_ENV_PATH};' \
              f'cm_ctl list --param --agent | grep agent_check_interval'
        self.log.info(cmd)
        show_param1 = self.user_nodes[0].sh(cmd).result()
        self.log.info(show_param1)
        self.assertEqual('2',
                         show_param1.splitlines()[0].split('=')[-1].strip(),
                         '参数默认值不为2')

        text = '-----step3:修改参数值为-1（上边界） expect:设置失败，合理报错-----'
        self.log.info(text)
        set_cmd = f'source {macro.DB_ENV_PATH};' \
                  f'cm_ctl set --param --agent -k "{self.param}=-1";'
        self.log.info(set_cmd)
        set_res = self.user_nodes[0].sh(set_cmd).result()
        self.log.info(set_res)
        self.assertIn(self.constant.cma_set_fail_msg, set_res, '参数设置成功')

        text = '-----step4:修改参数值为2147483648（下边界） expect:设置失败，合理报错-----'
        self.log.info(text)
        set_cmd = f'source {macro.DB_ENV_PATH};' \
                  f'gs_guc set -Z cmagent -N all -I all -c "{self.param} = 2147483648";'
        self.log.info(set_cmd)
        set_res = self.user_nodes[0].sh(set_cmd).result()
        self.log.info(set_res)
        self.assertIn('ERROR: The value 2147483648 is outside the valid range',
                      set_res, '参数设置成功')

        text = '-----step5:修改参数值为空（特殊字符） expect:设置失败，合理报错-----'
        self.log.info(text)
        set_cmd = f'source {macro.DB_ENV_PATH};' \
                  f'cm_ctl set --param --agent -k "{self.param}=''";'
        self.log.info(set_cmd)
        set_res = self.user_nodes[0].sh(set_cmd).result()
        self.log.info(set_res)
        self.assertIn('need input value', set_res, '参数设置成功')

        text = '-----step6:修改参数值为null（文本类型） expect:设置失败，合理报错-----'
        self.log.info(text)
        set_cmd = f'source {macro.DB_ENV_PATH};' \
                  f'gs_guc set -Z cmagent -c "{self.param} = null";'
        self.log.info(set_cmd)
        set_res = self.user_nodes[0].sh(set_cmd).result()
        self.log.info(set_res)
        self.assertIn('requires an integer value', set_res, '参数设置成功')

    def tearDown(self):
        text = '----poststep1:卸载cm;expect:执行成功----'
        self.log.info(text)
        self.un_cm = CmUnInstall(self.install_pkg_path,
                                 self.pri_sh.node.ssh_user)
        res = self.un_cm.cm_uninstall(
            self.user_nodes[0], '--deleteData --deleteBinary')
        self.assertIn('Uninstall CM tool success', res, f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
